package com.library.uiframework.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.v4.app.Fragment;
import android.util.Log;
import com.ali.music.navigator.backstack.AbstractFragment;
import com.library.uiframework.R;
import com.library.uiframework.fragment.BaseFragment;

/**
 * 在此写用途
 *
 * @version V1.0 <描述当前版本功能>
 * @FileName: com.youxue.listener.activity.WrapFragmentActivity.java
 * @author: zhiwei
 * @date: 2016-04-02 17:57
 */
public class WrapFragmentActivity extends BaseActivity {

    /**
     * 嵌套的fragment完整class name
     */
    public static final String KEY_CLASS_NAME = "key_class_name";

    /**
     * @return 包装的是哪个fragment
     */
    protected Class wrapFragmentClass() {
        return null;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        int requestIndex = requestCode>>16;
        //调用Fragment.startActivityForResult方法启动的
        if (requestIndex != 0) {
            return;
        }
        AbstractFragment fragment = getTopFragment();
        if(fragment != null){
            fragment.onActivityResult(requestCode, resultCode, data);
        }
    }

    public static void startSelf(Context context, Class clazz, Bundle bundle){
        Intent intent = new Intent(context, WrapFragmentActivity.class);
        intent.putExtra(KEY_CLASS_NAME, clazz.getCanonicalName());
        if(bundle != null){
            intent.putExtras(bundle);
        }
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    public static void startSelf(Activity context, Class clazz, Bundle bundle){
        Intent intent = new Intent(context, WrapFragmentActivity.class);
        intent.putExtra(KEY_CLASS_NAME, clazz.getCanonicalName());
        if(bundle != null){
            intent.putExtras(bundle);
        }
        context.startActivity(intent);
    }

    public static void startSelf(Fragment fragment, Class clazz, Bundle bundle){
        Intent intent = new Intent(fragment.getActivity(), WrapFragmentActivity.class);
        intent.putExtra(KEY_CLASS_NAME, clazz.getCanonicalName());
        if(bundle != null){
            intent.putExtras(bundle);
        }
        fragment.startActivity(intent);
    }

    public static void startSelf(android.app.Fragment fragment, Class clazz, Bundle bundle){
        Intent intent = new Intent(fragment.getActivity(), WrapFragmentActivity.class);
        intent.putExtra(KEY_CLASS_NAME, clazz.getCanonicalName());
        if(bundle != null){
            intent.putExtras(bundle);
        }
        fragment.startActivity(intent);
    }

    protected
    @LayoutRes
    int getContentLayoutId() {
        return R.layout.uiframework_activity_empty;
    }

    protected void wrapOncreate() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        wrapOncreate();
        setContentView(getContentLayoutId());
//        setLaunchFragmentAttr(R.id.layout_activity_container, R.anim.anim_action_menu_fade_out, R.anim.anim_action_menu_fade_in);
        setLaunchFragmentAttr(R.id.layout_activity_container, -1, -1);

        Class clazz = null;

        final Intent intent = getIntent();
        if (intent.hasExtra(KEY_CLASS_NAME)) {
            try {

                clazz = Class.forName(intent.getStringExtra(KEY_CLASS_NAME));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (clazz == null) {
            clazz = wrapFragmentClass();
        }
        if (clazz != null) {
            try {
                BaseFragment fragment = (BaseFragment) clazz.newInstance();
                fragment.setArguments(intent.getExtras());
                launchRootFragment(fragment, clazz.getName());
            } catch (Throwable e) {
                e.printStackTrace();
                Log.d("", "Error: 嵌套的class类型,必须是BaseFragment");
                finish();
            }
        } else {
            Log.d("", "Error: 请设置KEY_CLASS_NAME或者实现wrap");
            finish();
        }
    }
}
